package com.dearbinge.opentapi.content.withdrawDeposit;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.userWallet.UserWalletService;
import com.dearbinge.data.api.withdrawDeposit.WithdrawDepositService;
import com.dearbinge.data.pojo.userWallet.UserWallet;
import com.dearbinge.data.pojo.withdrawDeposit.WithdrawDeposit;
import com.dearbinge.data.redis.service.RedisService;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "c：用户提现")
@RestController
@CrossOrigin
@RequestMapping("/withdrawDeposit")
@EnableAutoConfiguration
public class WithdrawDepositController {

    @Autowired
    private UserWalletService userWalletService;

    @Autowired
    private RedisService redisService;


    @Autowired
    private WithdrawDepositService withdrawDepositService;

    /**
     * 根据用户ID在提现之前先拿到用户需要的数据
     */
    @ApiOperation(value = "根据用户ID在提现之前先拿到用户需要的数据",notes = "根据用户ID在提现之前先拿到用户需要的数据")
    @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")
    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
    public ResultMsg<Map<String,Object>> getByUserId(String userId){
        try {
            Map<String,Object> map = userWalletService.getByUserIdAnPhone(userId);
            return ResultMsg.ok(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody WithdrawDeposit withdrawDeposit){
        try {
            if (withdrawDeposit.getId() != null && withdrawDeposit.getId() != 0){
                Integer dataCode = withdrawDepositService.update(withdrawDeposit);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                UserWallet userWallet = userWalletService.getByUserId(withdrawDeposit.getUserId());
                if (userWallet.getUnfrozenMoney().compareTo(withdrawDeposit.getMoney()) < 0)return ResultMsg.error(502,"金额超出可提现金额");
                if (withdrawDeposit.getPhoneNumber() != null){
                    String sms = redisService.get(withdrawDeposit.getPhoneNumber() + "smsCode");
                    if (!sms.equals(withdrawDeposit.getVerificationCode()))return ResultMsg.error(501,"验证码不正确！");
                    withdrawDeposit.setStatus(withdrawDeposit.getStatus() == null ? 1 : withdrawDeposit.getStatus());
                    Integer dataCode = withdrawDepositService.insert(withdrawDeposit);
                    if (dataCode > 0){
                        return ResultMsg.ok("新增成功");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据id逻辑删除
     */
    @ApiOperation(value = "根据ID逻辑删除",notes = "根据ID逻辑删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = withdrawDepositService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
                        @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
                        @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query")
                })
    @RequestMapping(value = "/getByPage",method = RequestMethod.GET)
    public ResultMsg<Page> getByPage(String userId,Integer pageNo,Integer pageSize){
        try {
            Page page = withdrawDepositService.getByPage(userId,pageNo,pageSize);
            return ResultMsg.okData(page,"200");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


}
